﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cinser.Common;

namespace Cinser.DataTranslate.SettingManager
{
    public abstract class SettingBase
    {
        string id, settingName;

        public string Id
        {
            get
            {
                if (string.IsNullOrEmpty(id))
                    id = Guid.NewGuid().ToString();
                return id;
            }
            set { id = value; }
        }

        public string SettingName
        {
            get
            {
                if (string.IsNullOrEmpty(settingName))
                    settingName = this.Id;
                return settingName;
            }
            set { settingName = value; }
        }

        public abstract void SaveSettings();
        public virtual bool Delete()
        {
            bool bReturn = false;
            string xmlPath = GetSettingPath();
            Cinser.Common.XmlHelper xml = new Cinser.Common.XmlHelper(xmlPath);

            if (System.Windows.Forms.MessageBox.Show("是否确定要删除？") == System.Windows.Forms.DialogResult.OK)
            {
                //删除配置文件节点
                if (xml.FirstNode.FindNode("Item", "Id", this.Id, false) != null)
                {
                    xml.FirstNode.UpdateNode("Item", new List<string>() { "Id" }, new List<string>() { this.Id }, false, new XmlUtils.XmlNodeHandler((node) =>
                    {
                        //delete
                        node.ParentNode.RemoveChild(node);
                    }));
                    xml.XmlDocument.Save(xmlPath);
                    bReturn = true;
                }                
            }
            return bReturn;
        }
        public abstract string GetSettingPath();
    }
}
